﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using UcAsp.Models;
namespace UcAsp.Controllers
{
    public class ShowMessageController : Controller
    {
        //
        // GET: /ShowMessage/
        private UcAspNetEntities entities = new UcAspNetEntities();
        public ActionResult Index(int id)
        {
            try
            {
                if (id == 0)
                {
                    WorkFrame.Error.NoFound("文章不存");
                }
                Articles articles = entities.Articless.FirstOrDefault(o => o.ID == id);
                if (articles == null)
                {
                    WorkFrame.Error.NoFound("文章不存");
                }
                articles.Hot = articles.Hot + 1;
                entities.SaveChanges();
                Categories catgories = entities.Categoriess.FirstOrDefault(o => o.ID == articles.CategorieID);
                if (catgories.TmepPath.IndexOf(".cshtml", 0) < 0)
                {
                    WorkFrame.Error.NoFound("分类不允许在Message下浏览");

                }

                List<Articles> recommend = entities.Articless.Where(o => o.CategorieID == articles.CategorieID && o.IsRecommend == true).Take(6).ToList();
                List<Comments> comments = entities.Commentss.Where(o => o.ArticleID == id).ToList();


                ViewData["Next"] = entities.Articless.Where(o => o.ID > id && o.CategorieID == articles.CategorieID).OrderBy(o => o.ID).FirstOrDefault(); ;
                if (ViewData["Next"] == null)
                {
                    ViewData["Next"] = new Articles();
                }
                ViewData["Pre"] = entities.Articless.Where(o => o.ID < id && o.CategorieID == articles.CategorieID).OrderByDescending(o => o.ID).FirstOrDefault(); ;
                if (ViewData["Pre"] == null)
                {
                    ViewData["Pre"] = new Articles();
                }

                List<Comments> list = new List<Comments>();
                foreach (Comments c in comments)
                {
                    if (c.UserID == 1)
                    {
                        c.UserInfo = new UserInfo { UserName = "悠点网友", Logo = UcAsp.WorkFrame.SettingConfig.GetSiteSetting.UserHeader };
                    }
                    else
                    {
                        c.UserInfo = entities.UserInfos.FirstOrDefault(o => o.ID == c.UserID);
                        if (c.UserInfo == null)
                        {
                            c.UserInfo = new UserInfo { UserName = "悠点网友", Logo = UcAsp.WorkFrame.SettingConfig.GetSiteSetting.UserHeader };
                        }
                    }
                    c.Replay = entities.Commentss.Where(o => o.ParentID == c.ID).FirstOrDefault();
                    if (c.Replay != null)
                    {
                        c.Replay.UserInfo = new UserInfo { UserName = "悠点", Logo = UcAsp.WorkFrame.SettingConfig.GetSiteSetting.MasterHeader };
                    }
                    list.Add(c);
                }
                ShowMessageModel message = new ShowMessageModel { Art = articles, Recommend = recommend, Comments = list };
                if (Session["Info"] == null)
                {
                    message.Login = "User";
                }
                else
                {
                    message.Login = Session["Info"].ToString();
                }
                ViewData["IsLogin"] = true;
                ViewData["Point"] = true;
                //视频栏目
                if (articles.CategorieID == 15)
                {
                    if (Session["ID"] != null)
                    {
                        int userid = int.Parse(Session["ID"].ToString());
                        UserInfo user = entities.UserInfos.Where(o => o.ID == userid).FirstOrDefault();
                        //if (user.Point > 0||articles.Int1<1)
                        //{
                        //if (user.Point > articles.Int1 || articles.Int1 < 1)
                        // {
                        //  user.Point = user.Point - articles.Int1;

                        ViewData["IsLogin"] = true;
                        ViewData["Point"] = true;
                        //  entities.SaveChanges();
                        // }
                        //else {
                        //  ViewData["IsLogin"] = true;
                        // }
                        //}

                    }

                }

                return View(catgories.TmepPath, message);
            }
            catch {

                WorkFrame.Error.NoFound("分类不允许在Message下浏览");
                return View();
            }
        }
        public ActionResult Comments(ShowMessageModel model)
        {
            ViewData["ID"] = model.Art.ID.ToString();
            ViewData["Title"] = model.Art.Title.ToString();
            ViewData["Login"] = model.Login;
            return View("~/Views/ShowMessage/Comments.cshtml", model.Comments);
        }
    }
}
